package com.flainsky.nirvanaplayer.coder;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.ArrayList;
import java.util.List;

import com.flainsky.nirvanaplayer.lib.objects.Movie;

import android.util.Base64;

public class MovieObjectSerializable {
	static byte[] key=Base64.decode("YWJjZGVmZ2hpamtsbW5vcHFyc3R1dnd4".getBytes(),Base64.DEFAULT);
	public static String Encode(Object obj)
	{
		try {
	        ByteArrayOutputStream  baos=new ByteArrayOutputStream(); 
	        ObjectOutputStream objout = null;
	        objout = new ObjectOutputStream(baos);
	        objout.writeObject(obj);//写对象
	        byte[] b=Des3.des3EncodeECB(key, baos.toByteArray());
	        String s= new String(Base64.encode(b,  Base64.DEFAULT),"UTF-8");
	        objout.close();
	        baos.close();
	        return s;
		} catch (IOException e) {
        // TODO Auto-generated catch block
	        e.printStackTrace();
	        return "error";
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return "error";
		}
	}
	@SuppressWarnings("unchecked")
	public static List<Movie> MovieDecode(String obj)
	{
		byte[] b;
		try {
			b = Des3.ees3DecodeECB(key, Base64.decode(obj.getBytes("UTF-8"), Base64.DEFAULT));
	        ByteArrayInputStream bais=new ByteArrayInputStream(b);
	        ObjectInputStream ios=new ObjectInputStream(bais);
	        List<Movie> list =new ArrayList<Movie>();
	        list=(ArrayList<Movie>)ios.readObject();
	        //User user =(User) ios.readObject();
	        return list;
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}
}
